#include "MsgNode.h"
#include "const.h"
#include <boost/asio.hpp>
#include <iostream>

MsgNode::MsgNode(unsigned short max_len) : m_cur_len(0), m_total_len(max_len)
{
	m_data = new char[max_len + 1]();
	m_data[max_len] = '\0';
}

MsgNode::~MsgNode()
{
	std::cout << "destruct MsgNode" << std::endl;
	delete[] m_data;
}

void MsgNode::Clear() {
	::memset(m_data, 0, m_total_len);
	m_cur_len = 0;
}

SendNode::SendNode(const char* msg, unsigned short max_len, unsigned short msg_id) 
	: MsgNode(max_len + HEAD_TOTAL_LEN), m_msg_id(msg_id)
{
	 // tlv结构的结点
	// 先拷贝id并转为网络字节序
	short msg_id_host = boost::asio::detail::socket_ops::host_to_network_short(msg_id);
	memcpy(m_data, &msg_id_host, HEAD_ID_LEN);
	// 再拷贝消息体的长度并转为网络字节序
	short max_len_host = boost::asio::detail::socket_ops::host_to_network_short(max_len);
	memcpy(m_data + HEAD_ID_LEN, &max_len_host, HEAD_DATA_LEN);
	// 最后拷贝消息体
	memcpy(m_data + HEAD_ID_LEN + HEAD_DATA_LEN, msg, max_len);
}

RecvNode::RecvNode(unsigned short max_len, unsigned short msg_id) : MsgNode(max_len), m_msg_id(msg_id)
{

}